package com.tac.common.config;

import com.tac.common.interceptor.TokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


@Configuration
@EnableSwagger2
public class SpringMvcConfig  extends WebMvcConfigurationSupport {
    @Value("${shopping.path}")
    private String path;
    @Autowired
    private TokenInterceptor tokenInterceptor;
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tokenInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/doc.html","/webjars/**","/swagger-resources","/v2/api-docs"
                ,"/user/login","/user/register/**","/user/code","/category/selectAllByLevel"
                ,"/product/selectAll","/product/selectAllByCategoryId","/slide"
                ,"/file/**","/product/selectByName","/swagger-ui.html","/swagger-resources/**","/null/swagger-resources/**");
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        String pathUtl = "file:" + path.replace("\\","\\\\");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/file/**").addResourceLocations(pathUtl);
        //原生swagger

        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
    }

    //原生swagger
        @Bean

    public Docket getDocket() {
        // 文档类型
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.tac.controller"))//给controller生成测试接口文档
                .paths(PathSelectors.any())
                .build();
    }
    //swagger框架
//    @Bean
//    public Docket createRestApi() {
//        // 文档类型
//        return new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(apiInfo())
//                .select()
//                .apis(RequestHandlerSelectors.basePackage("com.tac.controller"))//给controller生成测试接口文档
//                .paths(PathSelectors.any())
//                .build();
//    }

    private ApiInfo apiInfo() {//接口文档信息
        return new ApiInfoBuilder()
                .title("shopping")
                .version("1.0")
                .description("接口文档")
                .build();
    }

}
